package ssi.peeno.managers;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SQLManager {
	private Connection connect;
	
	public SQLManager(String host, String database, String username, 
			String password) {
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			connect = DriverManager.getConnection("jdbc:mysql://" + host + 
					"/" + database, username, password);
			ConfigManager.log("Database connection created");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}
	
	public ResultSet returnQuery(String query) {
		ConfigManager.log(query);
		try {
			Statement s = connect.createStatement();
			s.executeQuery(query);
			return s.getResultSet();
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return null;
	}
	
	public int executeQuery(String query) {
		ConfigManager.log(query);
		try {
			return connect.createStatement().executeUpdate(query);
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
	}
	
	protected void close() {
		try {
			connect.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
